package com.put.mybills.activities;


import java.util.List;

import com.put.mybills.BaseDriveActivity;
import com.put.mybills.R;
import com.put.mybills.adapter.GlobalParams;
import com.put.mybills.cloud.SynchronizeAppData;
import com.put.mybills.cloud.SynchronizeBillPictures;
import com.put.mybills.cloud.SynchronizeFactureIcon;
import com.put.mybills.database.DatabaseHelper;
import com.put.mybills.model.Bill;
import com.put.mybills.model.Facture;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

public class CloudFragment extends Fragment {
	
	private static final String TAG = "CloudFragment";
	
	private Context context;

	private SharedPreferences sharedPreferences;
	private ImageButton syncFactureIcon;
	private ImageButton syncBillPictures;
	private ImageButton syncAppData;
	
	private TextView factureView;
	private TextView billView;
	private TextView appView;
	

	public CloudFragment(Context context) {		
		this.context = context;		
	
	}
	
	public CloudFragment() {}
	
	public static final CloudFragment newInstance()
	{
		CloudFragment f = new CloudFragment();	   
	    return f;
	}
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
    }

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		

		sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());			
		View rootView = inflater.inflate(R.layout.fragment_cloud, container, false);
		
		syncFactureIcon = (ImageButton) rootView.findViewById(R.id.cloud_sync_facture_icon);
		syncBillPictures= (ImageButton) rootView.findViewById(R.id.cloud_sync_bill_photos);
		syncAppData= (ImageButton) rootView.findViewById(R.id.cloud_sync_application_data);
		
		factureView = (TextView) rootView.findViewById(R.id.cloud_facture_icon);
		billView = (TextView) rootView.findViewById(R.id.cloud_bill_photos);
		appView = (TextView) rootView.findViewById(R.id.cloud_app_data);

		syncFactureIcon.setOnClickListener(new OnClickListener() {
	
			@Override
			public void onClick(View v) {	
				
				if (!BaseDriveActivity.mGoogleApiClient.isConnected())
				{
					notConnectedAlert();
					return;
				}
				
				
				SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
				String factureDataDir = sharedPref.getString(GlobalParams.getInstance().facture_icon_dir_sp, "");
				if (!factureDataDir.equals(""))
					new SynchronizeFactureIcon(getActivity()).synchronizeIcon(factureDataDir,factureView,getResources().getString(R.string.cloud_facture_store));
				else {
					notPreparedGoogleDrive();
				}
				
			}
		});
		
		
		syncBillPictures.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {	
				
				
				if (!BaseDriveActivity.mGoogleApiClient.isConnected())
				{
					notConnectedAlert();
					return;
				}			
				SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
				String billDataDir = sharedPref.getString(GlobalParams.getInstance().bill_picture_dir_sp, "");
				if (!billDataDir.equals(""))
					new SynchronizeBillPictures(getActivity()).synchronizeIcon(billDataDir,billView,getResources().getString(R.string.cloud_bill_stored));
				else {
					notPreparedGoogleDrive();
				}
			}
		});
		
		
		syncAppData.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {	
				
				if (!BaseDriveActivity.mGoogleApiClient.isConnected())
				{
					notConnectedAlert();
					return;
				}				
				
				SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());  
				String appDataDir = sharedPref.getString(GlobalParams.getInstance().app_data_dir_sp, "");
				if (!appDataDir.equals(""))
					new SynchronizeAppData(getActivity()).synchronizeAppData(appDataDir,appView,getResources().getString(R.string.cloud_data_store));
				else {
					notPreparedGoogleDrive();
				}
			}
		});
		
		setDataText();
		setFactureText();
		setBillText();
		
		
		if (sharedPreferences.getString("dir_id", "") == "") {
			//createdir.createFolder(sharedPreferences.getString("dir_name", "MyFacture"));			
		}
	  
		return rootView;			
	}
	
	public void setBillText() {
		
		int counter = 0;
		List<Bill> bills = GlobalParams.getInstance().billdatasource.getAllBills();
		for (Bill bill : bills) {
			if (bill.getDriveIDPicture() != null && !bill.getDriveIDPicture().equals(""))
				counter++;
		}
	
		billView.setText(getResources().getString(R.string.cloud_bill_stored) + String.valueOf(counter));
		
	}
	
	public void setFactureText() {
		
		int counter = 0;
		List<Facture> factures = GlobalParams.getInstance().facturedatasource.getAllFactures();
		for (Facture facture : factures) {
			if (facture.getDriveIDIcon() != null && !facture.getDriveIDIcon().equals(""))
				counter++;
		}
		
		factureView.setText(getResources().getString(R.string.cloud_facture_store) + counter);
		
		
	}
	
	
	public void setDataText() {
		
		int counter = 0;
		if (!GlobalParams.getInstance().facturedatasource.selectDriveIDAppData(DatabaseHelper.DATA_FACTURE).equals(""))
			counter++;
		
		if (!GlobalParams.getInstance().facturedatasource.selectDriveIDAppData(DatabaseHelper.DATA_BILL).equals(""))
			counter++;
		
		if (!GlobalParams.getInstance().facturedatasource.selectDriveIDAppData(DatabaseHelper.DATA_UNITS).equals(""))
			counter++;
		
		appView.setText(getResources().getString(R.string.cloud_data_store) + counter);
	
	}
	
	public void notConnectedAlert() {
		
		new AlertDialog.Builder(getActivity())
        .setIcon(android.R.drawable.ic_dialog_alert)	       
        .setMessage(R.string.cloud_notconnected)
        .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {}
        })  
        .show();
	
	}
	
	public void notPreparedGoogleDrive() {
		
		new AlertDialog.Builder(getActivity())
        .setIcon(android.R.drawable.ic_dialog_alert)	       
        .setMessage(R.string.cloud_nonprepared_GoogleDrive)
        .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {}
        })  
        .show();
	
	}
	

	

}
